package com.argrace.smart.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.argrace.smart.R;
import com.argrace.smart.adapter.MyRoomAdapter;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.fragment.RoomFragment;
import com.argrace.smart.model.LocalDb_RoomNameTable;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;

/**
 * Created by argrace_dev_1 on 2018/3/2.
 */

public class MyRoomActivity extends BaseActivity {
    private ListView listView;
    MyRoomAdapter adapter;
    private ArrayList<LocalDb_RoomNameTable> rooms = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myroom);
        init();
    }

    private void init() {
        setView();
        rooms.addAll(DataSupport.findAll(LocalDb_RoomNameTable.class));
        adapter.notifyDataSetChanged();
    }

    private void setView() {
        listView = (ListView) findViewById(R.id.listview);
        adapter = new MyRoomAdapter(this, rooms);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("name", rooms.get(position).getRoomName());
                intent.putExtra("id", rooms.get(position).getId());
                setResult(ConstantConfigure.RESULT_SUCCESS, intent);
                finish();
            }
        });
    }
}
